<template>
  <div class="app-container">
    <div class="filter-container">

<!--      <router-link :to="'/sku/create'">-->
      <router-link :to="{path:'/sku/create',query:{spuId:listQuery.spuId} }">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit">
          新增
        </el-button>
      </router-link>

    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="spuId" align="center">
        <template slot-scope="{row}">
          <span>{{ row.productId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.imgUrl" width="100px" height="100px">
        </template>
      </el-table-column>
      <el-table-column label="现价" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原价" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.originPrice }}</span>
        </template>
      </el-table-column>

      <el-table-column label="库存" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.stock }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <router-link :to="{path:'/sku/edit/'+scope.row.id,query:{spuId:listQuery.spuId} }">
            <el-button type="primary" size="small" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>

          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row.id)">
            删除
          </el-button>

        </template>

      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.curPageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { getSkuListBySpuId,deleteSku } from '@/api/sku'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Sku',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      listQuery: {
        curPageNum: 1,
        pageSize: 10,
        spuId: 0,
      },
      total: 0
    }
  },
  created() {
    const spuId = this.$route.params && this.$route.params.id
    this.listQuery.spuId = spuId
    this.getList(this.listQuery)
  },
  methods: {

    getList() {
      this.listLoading = true
      getSkuListBySpuId(this.listQuery).then(r1 => {
        console.log("返回：",r1.data)
        this.list = r1.data.list
        this.total = r1.data.total
        this.listLoading = false
      })
    },

    handleDelete(index,skuId) {
      console.log("index",index)
      console.log("skuId",skuId)
      this.$confirm('确定要删除该sku?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteSku(skuId)
          this.list.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(err => { console.error(err) })
    },

  }
}
</script>
